﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CodeTier.BUL;

namespace Quanly
{
    public partial class Frm_XuLyDonDatHang : Frm_Abstract
    {
        List<PhieuDatHangBUS> lst_pdh;

        public Frm_XuLyDonDatHang()
        {
            InitializeComponent();
            
            refresh();
        }

        public void loadData()
        {
            lst_pdh = PhieuDatHangBUS.getAll();            
        }

        public void show()
        {
            loadToListView_PDH();
        }

        public void loadToListView_PDH()
        {
            if (lst_pdh == null)
            {
                MessageBox.Show("Error load database", "Error Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                this.lstview_pdh.Items.Clear();
                foreach (PhieuDatHangBUS pdh in lst_pdh)
                {
                    addPDHIntoListview(pdh);
                }
            }
        }

        public void addPDHIntoListview(PhieuDatHangBUS pdh)
        {
            ListViewItem item;
            item = this.lstview_pdh.Items.Add(pdh.MaPDH.ToString());
            KhachHangBUS kh = new KhachHangBUS(pdh.MaKH);
            item.SubItems.Add(kh.HoTen);
            item.SubItems.Add(pdh.NgayDat.ToString());
            item.SubItems.Add(pdh.TongTriGia.ToString());
        }

        private void lstview_pdh_SelectedIndexChanged(object sender, EventArgs e)
        {
            PDHCurrent();
        }

        public void PDHCurrent()
        {
            if (lstview_pdh.SelectedItems.Count > 0)
            {
                List<ChiTietPhieuDatHangBUS> lst_ct = ChiTietPhieuDatHangBUS.getAll(Convert.ToInt32(lstview_pdh.SelectedItems[0].Text));
                loadToListView_CTPDH(lst_ct);
            }
            else
            {
                this.lstview_ctpdh.Items.Clear();
            }
        }

        public void loadToListView_CTPDH(List<ChiTietPhieuDatHangBUS> lst_ctpdh)
        {
            if (lstview_ctpdh == null)
            {
                MessageBox.Show("Error load database", "Error Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                this.lstview_ctpdh.Items.Clear();
                foreach (ChiTietPhieuDatHangBUS ctpdh in lst_ctpdh)
                {
                    addCTIntoListview(ctpdh);
                }
            }
        }

        public void addCTIntoListview(ChiTietPhieuDatHangBUS ctpdh)
        {
            ListViewItem item;
            item = this.lstview_ctpdh.Items.Add(ctpdh.MaCTPDH.ToString());
            SanPhamBUS sp = new SanPhamBUS(ctpdh.MaSP);
            item.SubItems.Add(sp.TenSP);
            item.SubItems.Add(ctpdh.SoLuong.ToString());
            item.SubItems.Add(ctpdh.DonGia.ToString());
            string temp = null;
            if (ctpdh.TinhTrang == "-1")
            {
                temp = "Chưa Xác Nhận";
            }
            else if (ctpdh.TinhTrang == "0")
            {
                temp = "Hủy";
            }
            else
            {
                temp = "Xác nhận";
            }
            item.SubItems.Add(temp);
        }

        private void lstview_ctpdh_SelectedIndexChanged(object sender, EventArgs e)
        {
            txt_mact.Text = lstview_ctpdh.SelectedItems[0].Text;

            List<status> lst_stt = new List<status>();
            lst_stt.Add(new status("Chưa Xác Nhận", -1));
            lst_stt.Add(new status("Hủy", 0));
            lst_stt.Add(new status("Xác Nhận", 1));

            cbo_TrangThai.DataSource = lst_stt;
            cbo_TrangThai.DisplayMember = "actionName";
            cbo_TrangThai.ValueMember = "actionValue";

            string stt = ChiTietPhieuDatHangBUS.selectByIdGetStatus(Convert.ToInt32(lstview_ctpdh.SelectedItems[0].Text));
            cbo_TrangThai.SelectedValue = Convert.ToInt32(stt);

        }

        private void bnt_XacNhan_Click(object sender, EventArgs e)
        {
            if (lstview_ctpdh.SelectedItems.Count > 0)
            {
                ChiTietPhieuDatHangBUS ct = new ChiTietPhieuDatHangBUS();
                ct.MaCTPDH = Convert.ToInt32(lstview_ctpdh.SelectedItems[0].Text);
                ct.TinhTrang = cbo_TrangThai.SelectedValue.ToString();
                ct.update();
                PDHCurrent();
                txt_mact.Text = null;
                cbo_TrangThai.DataSource = null;
                lstview_ctpdh.SelectedItems.Clear();
            }            
            
        }

        public override void refresh()
        {
            loadData();
            show();
        }

        public override void closeForm()
        {
            this.Close();
        }
    }

    public class status
    {
        public string actionName { get; set; }
        public int actionValue { get; set; }

        public status(string name, int value)
        {
            actionName = name;
            actionValue = value;
        }
    }

}